package com.fmsz.physicalheadline.activitys;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.fmsz.physicalheadline.BR;
import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.application.MyApplication;
import com.fmsz.physicalheadline.databinding.ActivityWelcomeBinding;
import com.fmsz.physicalheadline.entity.root.RootEntity;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WelcomeActivity extends AppCompatActivity implements Callback<RootEntity> {
    private final int TIME_UP = 1;
    private final int ONESEC = 0;
    private ActivityWelcomeBinding binding;
    private int COUNT = 4;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIME_UP:
                    Intent intent=new Intent(WelcomeActivity.this,BaseActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case ONESEC:
                    binding.secondNum.setText(String.valueOf(--COUNT));
                    break;
            }
        }
    };
    private Timer timer;
    private TimerTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_welcome);
        initData();
        finishThread();
    }

    private void finishThread() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                for (int i = 0; i < 4; i++) {
                    try {
                        Thread.sleep(1000);
                        handler.sendEmptyMessage(ONESEC);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                handler.sendEmptyMessage(TIME_UP);
            }
        };
        timer.schedule(task,0);

    }

    private void initData() {
        MyApplication application = (MyApplication) getApplication();
        application.getHttpService().getRoot().enqueue(this);
    }

    @Override
    public void onResponse(Call<RootEntity> call, Response<RootEntity> response) {
        RootEntity rootEntity = response.body();
        if (rootEntity!=null) {
            if (rootEntity.getCode()!=200) {
                return;
            }
            binding.setVariable(BR.welcome,rootEntity.getData());
        }
    }

    @Override
    public void onFailure(Call<RootEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
    public void clickFinish(View view){

//        isFinish = true;
//        Context context = view.getContext();
//        Intent intent = new Intent(context,BaseActivity.class);
//        context.startActivity(intent);
//        if (task!=null) {
//            task.cancel();
//            task=null;
//        }
//        if (timer!=null) {
//            timer.cancel();
//            timer=null;
//        }
//        ((WelcomeActivity) context).finish();
        
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
